package com.banmao.mall.controller.admin;

import com.banmao.mall.model.bo.admin.goods.GoodsBO;
import com.banmao.mall.model.bo.admin.goods.GoodsDetailBO;
import com.banmao.mall.model.bo.admin.goods.GoodsListBO;
import com.banmao.mall.model.vo.BaseRespVO;
import com.banmao.mall.model.vo.admin.goods.CatAndBrandVO;
import com.banmao.mall.model.vo.admin.goods.GoodsDetailVO;
import com.banmao.mall.model.vo.admin.goods.GoodsListVO;
import com.banmao.mall.service.admin.GoodsService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/admin")
public class GoodsController {

    @Autowired
    GoodsService goodsService;

    // 获取商品列表
    @RequiresPermissions("admin:goods:list")
    @RequestMapping("/goods/list")
    public BaseRespVO goodsList(GoodsListBO goodsListBO) {

        GoodsListVO goodsListVO = goodsService.goodsList(goodsListBO);
        if(goodsListVO == null) {
            return BaseRespVO.error("服务器抽风啦", 401);
        }

        return BaseRespVO.ok(goodsListVO);
    }


    //获取商品类目及品牌商

    @RequestMapping("/goods/catAndBrand")
    public BaseRespVO catAndBrand() {

        CatAndBrandVO catAndBrandVO = goodsService.catAndBrand();

        return BaseRespVO.ok(catAndBrandVO);
    }


    // 创建商品
    @RequiresPermissions("admin:goods:create")
    @RequestMapping("/goods/create")
    public BaseRespVO goodsCreate(@RequestBody GoodsDetailBO goodsDetailBO) {

        System.out.println(goodsDetailBO);

        try {
            int result = goodsService.goodsCreate(goodsDetailBO);
        } catch (Exception e) {
            e.printStackTrace();
            return BaseRespVO.error("添加失败！", 401);
        }

        return BaseRespVO.ok(null);
    }


    // 获取商品详情信息
    @RequestMapping("/goods/detail")
    @RequiresPermissions("admin:goods:read")
    public BaseRespVO goodsDetail(@RequestParam("id") Integer goodsId) {

        GoodsDetailVO goodsDetailVO = goodsService.goodsDetail(goodsId);
        if(goodsDetailVO == null) {
            return BaseRespVO.error("服务器抽风啦！", 401);
        }

        return BaseRespVO.ok(goodsDetailVO);
    }


    // 更新商品信息
    @RequestMapping("/goods/update")
    @RequiresPermissions("admin:goods:update")
    public BaseRespVO goodsUpdate(@RequestBody GoodsDetailBO goodsDetailBO) {

        try {
            int result = goodsService.goodsUpdate(goodsDetailBO);
        } catch (Exception e) {
            e.printStackTrace();
            return BaseRespVO.error("插入失败啦", 401);
        }

        return BaseRespVO.ok(null);
    }


    // 删除商品
    @RequestMapping("/goods/delete")
    @RequiresPermissions("admin:goods:delete")
    public BaseRespVO goodsDelete(@RequestBody GoodsBO goodsBO) {

        try {
            int result = goodsService.goodsDelete(goodsBO);
        } catch (Exception e) {
            e.printStackTrace();
            return BaseRespVO.error("删除失败啦！", 401);
        }

        return BaseRespVO.ok(null);
    }

}
